package engine.graphics.sprite
{
	import engine.asset.*;
	import engine.core.*;
	import engine.graphics.anim.*;
	import engine.graphics.resources.*;
	
	import flash.geom.*;
	
	public class RxAnimTextureSheet
	{
		private var m_spriteSheet:RxAssetSpriteSheet;
		private var m_spriteSet:RxSpriteSet;
		private var m_frameAnimation:RxFrameAnimation;
		private var m_rcpWidth:Number;
		private var m_rcpHeight:Number;
		private var m_currFrameRect:Rectangle;
		private var m_nextFrameRect:Rectangle;
		private static var s_pool:RxObjectPool = null;
		
		public function RxAnimTextureSheet(sheet:RxAssetSpriteSheet)
		{
			Init(sheet);
		}
		
		public function Init(spriteSheet:RxAssetSpriteSheet) : void
		{
			var currentRectangle:Rectangle = null;
			var nextRectangle:Rectangle = null;
			this.m_spriteSheet = spriteSheet;
			this.m_spriteSet = this.m_spriteSheet != null ? (this.m_spriteSheet.GetSet(null)) : (null);
			if (this.m_frameAnimation == null)
			{
				this.m_frameAnimation = new RxFrameAnimation();
			}
			if (this.m_currFrameRect == null)
			{
				this.m_currFrameRect = new Rectangle();
			}
			if (this.m_nextFrameRect == null)
			{
				this.m_nextFrameRect = new Rectangle();
			}
			if (this.m_spriteSet != null)
			{
				this.m_frameAnimation.Init(this.m_spriteSet.frames, this.m_spriteSet.frameTime);
				this.m_rcpWidth = 1 / Number(this.m_spriteSet.setWidth);
				this.m_rcpHeight = 1 / Number(this.m_spriteSet.setHeight);
				currentRectangle = this.m_spriteSet.GetFrameRect(this.m_frameAnimation.GetCurrentFrame());
				nextRectangle = this.m_spriteSet.GetFrameRect(this.m_frameAnimation.GetNextFrame());
				this.NormalizeRect(this.m_currFrameRect, currentRectangle);
				this.NormalizeRect(this.m_nextFrameRect, nextRectangle);
			}
		}
		
		private function NormalizeRect(frameRect:Rectangle, rect:Rectangle) : void
		{
			frameRect.x = rect.x * this.m_rcpWidth;
			frameRect.y = rect.y * this.m_rcpHeight;
			frameRect.width = rect.width * this.m_rcpWidth;
			frameRect.height = rect.height * this.m_rcpHeight;
		}
		
		public static function CreatePool(initSize:uint, growSize:uint) : void
		{
			var allocFunc:* = function () : Object
			{
				return new RxAnimTextureSheet(null);
			};
			s_pool = new RxObjectPool(allocFunc, null, initSize, growSize, RxAnimTextureSheet);
		}
	}
}